package com.example.newheze.Monitor;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;

import com.example.newheze.FieldMessage.AnalogData;
import com.example.newheze.FieldMessage.CurrentData;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.vo.Info;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MonitorService extends Service {
    CurrentData currentData;
    Info data;
    private String[] date_soil_humi1= new String[7];
    private float[] info_soil_humi1=new float[7];
    private String[] date_soil_temp1= new String[7];
    private float[] info_soil_temp1=new float[7];
    private String[] date_soil_elect1= new String[7];
    private float[] info_soil_elect1=new float[7];
    private String[] date_soil_humi2= new String[7];
    private float[] info_soil_humi2=new float[7];
    private String[] date_soil_temp2= new String[7];
    private float[] info_soil_temp2=new float[7];
    private String[] date_soil_elect2= new String[7];
    private float[] info_soil_elect2=new float[7];
    private String[] date_sunlight= new String[7];
    private float[] info_sunlight=new float[7];
    private String[] date_co2= new String[7];
    private float[] info_co2=new float[7];
    private String[] date_air_humi= new String[7];
    private float[] info_air_humi=new float[7];
    private String[] date_air_temp= new String[7];
    private float[] info_air_temp=new float[7];

    public MonitorService() {

    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        executeNetListen();
        Log.d("myservice", "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }
    private void executeNetListen() {
        new MyAsyncTask().execute("http://202.194.131.174:8080/wudihoutai/Wenshi_wudi_1_servlet?DTU=001&num=7");

//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//                new MyAsyncTask().execute("http://202.194.131.174:8080/wudihoutai/Wenshi_wudi_1_servlet?DTU=001&num=7");
//                executeNetListen();
//            }
//        }, 300000);  //每5min执行一次HTTP请求并判断结果
    }
//    private void downloadData(){
//
//        AnalogData analogData=new AnalogData();
//
//        Bundle mBundle = new Bundle();
//        mBundle.putParcelable("Data", analogData);//压入数据
//        Intent intent = new Intent();
//        intent.setClass(MonitorService.this, MessageActivity.class);
//        intent.putExtras(mBundle);
//        startActivity(intent);
//    }

    public class MyAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    data = new Info(jsonObject.getString("datetime"), jsonObject.getString("soil_humi1"), jsonObject.getString("soil_temp1"), jsonObject.getString("soil_elect1"), jsonObject.getString("soil_humi2"), jsonObject.getString("soil_temp2"), jsonObject.getString("soil_elect2"), jsonObject.getString("sunlight"), jsonObject.getString("co2"), jsonObject.getString("air_humi"), jsonObject.getString("air_temp"), jsonObject.getString("DTU"));
                    date_soil_humi1[i]=data.getDatetime();
                    info_soil_humi1[i]= Float.parseFloat(data.getSoil_humi1());
                    date_soil_temp1[i]=data.getDatetime();
                    info_soil_temp1[i]= Float.parseFloat(data.getSoil_temp1());
                    date_soil_elect1[i]=data.getDatetime();
                    info_soil_elect1[i]= Float.parseFloat(data.getSoil_elect1());
                    date_soil_humi2[i]=data.getDatetime();
                    info_soil_humi2[i]= Float.parseFloat(data.getSoil_humi2());
                    date_soil_temp2[i]=data.getDatetime();
                    info_soil_temp2[i]= Float.parseFloat(data.getSoil_temp2());
                    date_soil_elect2[i]=data.getDatetime();
                    info_soil_elect2[i]= Float.parseFloat(data.getSoil_elect2());
                    date_sunlight[i]=data.getDatetime();
                    info_sunlight[i]= Float.parseFloat(data.getSunlight());
                    date_co2[i]=data.getDatetime();
                    info_co2[i]= Float.parseFloat(data.getCo2());
                    date_air_humi[i]=data.getDatetime();
                    info_air_humi[i]= Float.parseFloat(data.getAirhumi());
                    date_air_temp[i]=data.getDatetime();
                    info_air_temp[i]= Float.parseFloat(data.getAirtemp());
                }
                AnalogData analogData=new AnalogData();

                analogData.setDate_soil_humi1(date_soil_humi1);
                analogData.setInfo_soil_humi1(info_soil_humi1);
                analogData.setDate_soil_temp1(date_soil_temp1);
                analogData.setInfo_soil_temp1(info_soil_temp1);
                analogData.setDate_soil_elect1(date_soil_elect1);
                analogData.setInfo_soil_elect1(info_soil_elect1);
                analogData.setDate_soil_humi2(date_soil_humi2);
                analogData.setInfo_soil_humi2(info_soil_humi2);
                analogData.setDate_soil_temp2(date_soil_temp2);
                analogData.setInfo_soil_temp2(info_soil_temp2);
                analogData.setDate_soil_elect2(date_soil_elect2);
                analogData.setInfo_soil_elect2(info_soil_elect2);
                analogData.setDate_sunlight(date_sunlight);
                analogData.setInfo_sunlight(info_sunlight);
                analogData.setDate_co2(date_co2);
                analogData.setInfo_co2(info_co2);
                analogData.setDate_air_humi(date_air_humi);
                analogData.setInfo_air_humi(info_air_humi);
                analogData.setDate_air_temp(date_air_temp);
                analogData.setInfo_air_temp(info_air_temp);

//                Bundle mBundle = new Bundle();
//                mBundle.putParcelable("Data", analogData);//压入数据
//                Intent intent = new Intent();
//                intent.setClass(MonitorService.this,MessageActivity.class);
//                intent.putExtras(mBundle);
//                startActivity(intent);
                onDestroy();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.sendPostMethod(strings[0],"utf-8");
        }
    }

}
